<template>
  <div :class="className" :style="{height:height,width:width}" />
</template>

<script>
import echarts from 'echarts' // echarts theme
import resize from '@/framework/utils/mixins/resize'
import { statisticalAuth } from '@/framework/api/authenticationCenter/index'

require('echarts/theme/macarons')

const animationDuration = 6000

export default {
  mixins: [resize],
  props: {
    className: {
      type: String,
      default: 'chart'
    },
    width: {
      type: String,
      default: '100%'
    },
    height: {
      type: String,
      default: '300px'
    }
  },
  data() {
    return {
      chart: null,
      param: {
        beginTime: '',
        endTime: ''
      },
      yData: [],
      xData: []
    }
  },
  beforeDestroy() {
    if (!this.chart) {
      return
    }
    this.chart.dispose()
    this.chart = null
  },
  methods: {
    initChart(beginTime, endTime) {
      this.param.beginTime = beginTime
      this.param.endTime = endTime
      statisticalAuth(this.param).then((res) => {
        if (res.success) {
          this.chart = echarts.init(this.$el, 'macarons')
          this.chart.setOption({
            title: {
              text: '认证情况',
              show: false,
              x: 'left',
              y: 'top',
              textStyle: {
                color: '#131314',
                fontSize: '16'
              }
            },
            tooltip: {
              trigger: 'axis',
              axisPointer: { // 坐标轴指示器，坐标轴触发有效
                type: 'shadow' // 默认为直线，可选为：'line' | 'shadow'
              }
            },
            grid: {
              top: '12%',
              left: '2%',
              right: '2%',
              bottom: '10%',
              containLabel: true
            },
            xAxis: [{
              type: 'category',
              data: res.data.keys,
              axisTick: {
                alignWithLabel: true
              },
              axisLabel: {
                textStyle: {
                  color: '#86909C'
                }
              },
              axisLine: {
                // show: false,
                lineStyle: {
                  color: '#E5E6EB'
                }
              }
            }],
            yAxis: [{
              type: 'value',
              axisTick: {
                show: false
              },
              splitArea: { show: false }, // 'areaStyle': { 'color': ['#fff'] }
              splitLine: { // 去除网格线
                show: true,
                lineStyle: {
                  color: '#E5E8EF',
                  type: 'dashed'
                }
              },
              axisLabel: {
                textStyle: {
                  color: '#86909C'
                }
              },
              axisLine: {
                show: false,
                lineStyle: {
                  color: '#86909C'
                }
              }
            }],
            color: ['#20CCFF'],
            series: [{
              name: '认证次数',
              type: 'bar',
              stack: 'vistors',
              barWidth: '16',
              data: res.data.values,
              animationDuration,
              itemStyle: {
                show: false,
                normal: {
                  areaStyle: {
                    opacity: 1
                  }
                }
              }
            }
            ]
          })
        }
      })
    }

  }
}
</script>
